package com.huang.demo1;

//基本买票问题
// oop模式 调用对象
public class SaleTickerDemo1 {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(()->{
            for (int i = 0; i < 20; i++) {
                ticket.buy();
            }

        },"A").start();

    new Thread(()->{
        for (int i = 0; i < 20; i++) {
            ticket.buy();
        }
    },"B").start();

    new Thread(()->{
        for (int i = 0; i < 20; i++) {
            ticket.buy();
        }
        },"C").start();
        }


}

class Ticket{
    private int number=10;
   /*
    // synchronized  排队
     //
    // */
    public synchronized void buy(){
        if(number>0){
            System.out.println(Thread.currentThread().getName()+"--"+"卖出了第"+number--+"张票"+"剩余"+number);
        }
    }

}